package com.javaEE.tes.controllers;

import com.javaEE.tes.dao.CurriculumRepository;
import com.javaEE.tes.dao.dto.CourseSelectionDto;
import com.javaEE.tes.dao.dto.StudentClass1;
import com.javaEE.tes.entities.*;
import com.javaEE.tes.services.CourseSelectionService;
import com.javaEE.tes.services.CurriculumService;
import com.javaEE.tes.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("courseSelection")
public class CourseSelectionControllers {
    @Autowired
    CourseSelectionService courseSelectionService;
    @Autowired
    CurriculumService curriculumService;
    @Autowired
    StudentService studentService;
    @GetMapping
    public String displayCourseSelection(Model model){
        List<CourseSelectionDto> courseSelectionDtos= courseSelectionService.courseSelection();
        model.addAttribute("courseSelectionDtos",courseSelectionDtos);
        return "courseSelection/list-courseSelection";
    }
    @GetMapping("/new")
    public String displayCourseSelectionForm(Model model){

        CourseSelection courseSelection = new CourseSelection();
        model.addAttribute("courseSelection",courseSelection);
        List<Curriculum> curriculums =curriculumService.getAll();
        List<Student>  students = studentService.getAll();
        model.addAttribute("aCurriculum",curriculums);
        model.addAttribute("aStudents",students);
        return "courseSelection/new-courseSelection";
    }
    @PostMapping("/save")
    public String createCourseSelection(Model model, @Valid CourseSelection courseSelection, Errors errors){
//        List<Curriculum> curriculums =curriculumService.getAll();
//        List<Student>  students = studentService.getAll();
//        model.addAttribute("aCurriculum",curriculums);
//        model.addAttribute("aStudents",students);

        if (errors.hasErrors()) return "courseSelection/new-courseSelection";

        courseSelectionService.save(courseSelection);
        return "redirect:/courseSelection";
    }

}
